package contraGame;

import java.awt.Color;

import main.Board;
import quadtree.DoublePoint;
import utility.ContraBullit;
import utility.ContraEnemy;
import utility.ContraMan;
import utility.ContraPlatform;
import utility.ContraReticle;

public class ContraBoard extends Board {
	
	private ContraMan bob;
	//private ContraBullit contraBullit;
	private ContraReticle contraReticle;

	ContraBoard() {
		super(800, 600 + 50, Color.BLUE); //the 50 is to offset the weird
		//cut off point the jFrame does
		super.setDebug(false);
		gameInit();
		setCursorImage("");
	}

	public void gameInit() {
		bob = new ContraMan(30, 550);
		bob.setGameTree(gameTree);
		bob.createContraMansGun();
		gameTree.insertSprite(new DoublePoint(30,550), bob);
		contraReticle = new ContraReticle();
		gameTree.insertSprite(new DoublePoint(30,550), contraReticle);
		gameTree.insertSprite(new DoublePoint(75, 555), 
				new ContraPlatform(75, 555, 100, 25, Color.DARK_GRAY));
		gameTree.insertSprite(new DoublePoint(225, 545), 
				new ContraPlatform(225, 545, 100, 25, Color.DARK_GRAY));
		gameTree.insertSprite(new DoublePoint(550, 225), 
				new ContraEnemy(550, 225, 25, 25, Color.PINK));
		gameTree.insertSprite(new DoublePoint(150, 350), 
				new ContraEnemy(150, 350, 25, 25, Color.PINK));
	}


	/* To add a key handler,  you must also modify the TAdapter 
	 * and InputHandler classes
	 */
	public void gameHandleInput() {
		if(userInput.inputHandler.DOWN_PUSH == true || 
				userInput.inputHandler.S_PUSH == true){
			bob.keyPressed("VK_DOWN");
		}
		if(userInput.inputHandler.UP_PUSH == true ||
				userInput.inputHandler.W_PUSH == true){
			//not handled yet
		}
		if(userInput.inputHandler.RIGHT_PUSH == true ||
				userInput.inputHandler.D_PUSH == true){
			bob.keyPressed("VK_RIGHT");
		}	
		if(userInput.inputHandler.LEFT_PUSH == true ||
				userInput.inputHandler.A_PUSH == true){
			bob.keyPressed("VK_LEFT");
		}
		if(userInput.inputHandler.SPACE_PUSH == true){
			bob.keyPressed("VK_SPACE");
		}
		if(userInput.inputHandler.SHIFT_PUSH == true){
			//not handled yet
		}
		if(userInput.inputHandler.DOWN_RELEASE == true ||
				userInput.inputHandler.S_RELEASE== true){
			bob.keyReleased("VK_DOWN");
			userInput.inputHandler.DOWN_RELEASE = false;
			userInput.inputHandler.S_RELEASE = false;
		}
		if(userInput.inputHandler.UP_RELEASE == true ||
				userInput.inputHandler.W_RELEASE == true){
			//not handled yet
			userInput.inputHandler.UP_RELEASE = false;
			userInput.inputHandler.W_RELEASE = false;
		}
		if(userInput.inputHandler.RIGHT_RELEASE== true ||
				userInput.inputHandler.D_RELEASE== true){
			bob.keyReleased("VK_RIGHT");
			userInput.inputHandler.RIGHT_RELEASE = false;
			userInput.inputHandler.D_RELEASE = false;
		}	
		if(userInput.inputHandler.LEFT_RELEASE == true ||
				userInput.inputHandler.A_RELEASE== true){
			bob.keyReleased("VK_LEFT");
			userInput.inputHandler.LEFT_RELEASE = false;
			userInput.inputHandler.A_RELEASE = false;
		}
		if(userInput.inputHandler.SPACE_RELEASE == true){
			//does nothing
			userInput.inputHandler.SPACE_RELEASE = false;
		}
		if(userInput.inputHandler.SHIFT_RELEASE == true){
			userInput.inputHandler.SHIFT_RELEASE = false;
		}
		if(userInput.inputHandler.MOUSE_MOVED == true) {
			userInput.inputHandler.MOUSE_MOVED = false;
			contraReticle.setX(userInput.MouseX);
			contraReticle.setY(userInput.MouseY);
			mouseMoved(userInput.MouseX, userInput.MouseY);
		}
		if(userInput.inputHandler.MOUSE_PRESSED == true) {
			userInput.inputHandler.MOUSE_PRESSED = false;
			if(userInput.inputHandler.MOUSE_LEFT_CLICK) {
				userInput.inputHandler.MOUSE_LEFT_CLICK = false;
				userInput.inputHandler.MOUSE_LEFT_CLICK = false;
				mousePressed(userInput.MouseX, userInput.MouseY);
				contraReticle.setWidth(5);
			}
			else if(userInput.inputHandler.MOUSE_RIGHT_CLICK) {
				userInput.inputHandler.MOUSE_RIGHT_CLICK = false;
			}
		}
	}

	public void generateBackground(){
		
	}

	public void handleInput(){
		try{
			gameHandleInput();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void mousePressed(int X, int Y) {
		//super.mouseClicked(e);
		// TODO Auto-generated method stub
		ContraBullit tmp = new ContraBullit(bob.gunOfContraMan.getXChange() + bob.gunOfContraMan.getX(), bob.gunOfContraMan.getYChange() + bob.gunOfContraMan.getY(), 
				bob.gunOfContraMan.getXDirection() * (int) (5 * Math.cos(bob.gunOfContraMan.getRadians())), 
				bob.gunOfContraMan.getYDirection() * (int) (5 * Math.sin(bob.gunOfContraMan.getRadians())));
		gameTree.insertSprite(new DoublePoint(bob.gunOfContraMan.getX(), bob.gunOfContraMan.getY()), 
				tmp);
		tmp = null;
	}
	
	public void mouseMoved(int X, int Y) {
		// TODO Auto-generated method stub
		bob.gunOfContraMan.setEndX(X);
		bob.gunOfContraMan.setEndY(Y);
	}
	
}
